<!-- 提交报价 -->
<template>
	<ListLayout>
		<QuoteCard ref="QuoteCard"></QuoteCard>
		<AttachmentCard :data="data" ref="AttachmentCard" tips="可上传报价单，维修清单等单据"></AttachmentCard>
		<template #footer>
			<HButtonGroup @leftClick="onCancel" @rightClick="onSubmit">
				<template #left>
					取消
				</template>
				<template #right>
					提交报价
				</template>
			</HButtonGroup>
		</template>
	</ListLayout>

</template>

<script>
	import ListLayout from '@/components/layout/listLayout.vue'
	import QuoteCard from '@/components/card/sendRepair/factory/quote.vue'
	import AttachmentCard from '@/components/card/attachment.vue'
	import HButtonGroup from '@/components/buttonGroup/groupTwo.vue'
	import ReapirApi from '@/apis/repair.js'
	import { attachmentFormatter } from '@/common/submitFormatter.js'
	import SendRepairRouterConfig from '@/sendRepair/routerConfig.js'
	export default {
		components: {
			ListLayout,
			QuoteCard,
			AttachmentCard,
			HButtonGroup
		},
		data() {
			return {
				data: {},
				routerParams: {
					id: ''
				}
			}
		},

		methods: {
			onCancel() {
				wx.navigateBack()
			},
			async onSubmit() {
				const { QuoteCard, AttachmentCard } = this.$refs
				const { attachment } = AttachmentCard.$data
				const { price } = QuoteCard.$data
				if (!price) {
					wx.showToast({
						title: '报价金额不能为空',
						icon: 'error'
					})
					return
				}
				let params = {
					repairId: this.routerParams.id,
					quoteMoney: price,
					remark: '',
					enclosure: attachmentFormatter(attachment)
				}
				try {
					await ReapirApi.factory.submitQuote(params)
					wx.showToast({
						title: '报价成功',
						icon: 'success',
						complete() {
							wx.redirectTo({
								url: SendRepairRouterConfig.Factory.Index
							})
						}
					})
				} catch ({
					message
				}) {
					wx.showToast({
						title: message,
						icon: 'error'
					})
				}


			}
		},
		async onLoad(query) {
			const {
				id
			} = query
			this.$set(this.routerParams, 'id', id)
			let res = await ReapirApi.common.getDetail(id)
			if (res.code === 0) {
				this.data = res.body
			}
		},
	}
</script>

<style lang="scss" scoped>
	.m {
		&-t {
			&-30 {
				margin-top: 30rpx;
				display: block;
			}
		}
	}
</style>
